package api

import (
	"Walker/app/services"
	"Walker/app/utils/data"
	"Walker/app/utils/response"
	"Walker/global"
	"github.com/gin-gonic/gin"
	"github.com/gomodule/redigo/redis"
	"github.com/streadway/amqp"
)

type Rabbit struct {
	RabbitService services.RabbitService
}

//这里是发送队列
func (controller *Rabbit) Index(ctx *gin.Context) {
	conn := global.ConfigRedis.Get()
	reply, err := redis.Int(conn.Do("Incr", "num"))
	if err != nil {
		response.Error(ctx, err.Error())
	}

	queue, channel := controller.RabbitService.Conn()
	body := "你是傻逼" + data.IntToString(reply) + "号"
	//把数据存入redis表明发送了多少条数据
	redis.Int(conn.Do("Lpush", "publisher", body))
	channel.Publish("", queue.Name, false, false, amqp.Publishing{
		ContentType: "text/plain",
		Body:        []byte(body),
	})
	return
}
